package db

import (
	db2 "common/db"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

type Config struct {
	Database string `help:"数据库连接"  devDefault:"mysql://ljg:abcd123456@tcp(192.168.43.90:3306)/wx?charset=utf8mb4&parseTime=True&loc=Local" default:"mysql://ljg:abcd123456@tcp(192.168.43.90:3306)/abcd?charset=utf8mb4&parseTime=True&loc=Local"`
	LogLevel string `help:"sql日志打印级别" default:"info"`
}

type CenterDB struct {
	log *zap.Logger
	db  *gorm.DB

	nodeCache *nodeCache
}

func NewDBLayer(databaseUrl string, log *zap.Logger, logLevel string) (*CenterDB, error) {
	db, err := db2.NewGormDBWithLog(databaseUrl, log, logLevel)
	if err != nil {
		return nil, err
	}
	cdb := &CenterDB{db: db, log: log}
	return cdb, nil
}

// Transaction 事务操作
func (d *CenterDB) Transaction(fn func(tx *gorm.DB) error) error {
	return d.db.Transaction(func(tx *gorm.DB) error {
		return fn(d.db)
	})
}
